<?php
declare (strict_types = 1);

namespace app\model;

use think\Model;

/**
 * @property bool $status 状态:0=审核中,1=审核成功,2=审核失败,
 * @property float $latitude 纬度
 * @property float $longitude 经度
 * @property int $browse 浏览数量
 * @property int $class_id 分类ID
 * @property int $hot 热度
 * @property int $id
 * @property int $users_id 用户
 * @property string $address 商户地址
 * @property string $content 商家介绍
 * @property string $createtime
 * @property string $hk_area_ids
 * @property string $images 商家相册
 * @property string $logo_image 商家logo
 * @property string $mobile 电话
 * @property string $open_times 营业时间
 * @property string $store_rule 商业规模
 * @property string $title 商家名称
 * @property string $type 所属行业
 * @property string $unique 第三方唯一标识
 * @property-read \app\model\StoreClass $class
 * @mixin \think\Model
 */
class Store extends Model
{
    protected $createTime = 'createtime';


    public static function onBeforeInsert(Model $model): mixed
    {
        if (empty($model->getAttr('images'))){
            $images = Config::where(['name'=>'store'])->value('value');
            $model->setAttr('images',$images);
        }
        return true;
    }

    function class()
    {
        return $this->belongsTo(StoreClass::class,'class_id');
    }

    function getImagesAttr($value)
    {
        if (empty($value)){
            return '';
        }
        $value = explode(',',$value);
        foreach ($value as &$v){
            if (!str_contains($v, "http")) {
                $v=config('app.static_host').$v;
            }
        }
        return implode(',',$value);
    }

    function getLogoImageAttr($value)
    {
        if (empty($value)){
            return '';
        }
        $value = explode(',',$value);
        foreach ($value as &$v){
            if (!str_contains($v, "http")) {
                $v=config('app.static_host').$v;
            }
        }
        return implode(',',$value);
    }
}
